package com.erp.framework.web.service;

import com.erp.framework.domain.SysDictData;
import com.erp.framework.service.ISysDictDataService;
import com.erp.framework.service.ISysDictTypeService;
import com.erp.framework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * html调用 thymeleaf 实现字典读取
 *
 * @author erp
 */
@Service("dict")
public class DictService {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<SysDictData> getType(String dictType) {
        return dictTypeService.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel(String dictType, String dictValue) {
        SysDictData param = new SysDictData();
        param.setDictValue(dictValue);
        param.setDictType(dictType);
        return dictDataService.list(param).stream().map(SysDictData::getDictLabel).findFirst().orElse(StringUtils.EMPTY);
    }
}
